<template>
	<view class="shoppingCart">
		<NavBar title="购物车">
		<!-- 	<template #right>
				<view class="topRight" @tap="navTo('/pages/shoppingCartSearch/index')">
					<image src="/static/img/index/111.png" mode="widthFix"></image>
					管理
				</view>
			</template> -->
		</NavBar>
		<view class="top_img">
			<image src="/static/img/index/107.png" mode="widthFix"></image>
		</view>
		<scroll-view scroll-y="true">
			<view class="scrollView">
				<view class="shoppingCartItem" v-for="(item,index) in data.result.list" :key="index">
					<view @tap="handeAllItem(item)" class="top">
						<image
							:src="item.products.filter(item=>!item.bool).length?'/static/img/index/52.png':'/static/img/index/49.png'"
							mode="widthFix"></image>
						{{item.storeName}}
					</view>
					<view @longpress="longpress(item,index,index1)" @tap="item1.bool = !item1.bool" class="shoppingCartItem_1"
						v-for="(item1,index1) in item.products" :key="index1">
						<image :src="item1.bool?'/static/img/index/49.png':'/static/img/index/52.png'" mode="widthFix">
						</image>
						<view class="right">
							<view class="left">
								<text>{{item1.sellType.name}}</text>
									<image :src="data.httpUrl+'/app/file/viewFile?fileId='+item1.fileId" mode="aspectFill"></image>
							</view>
							<view class="right">
								<text class="ellipsis_2">{{item1.productName}}</text>
								<text>{{item1.specName}}：{{item1.specValue}} × {{item1.num}}</text>
								<view class="attr">
									<text class="attr" v-for="(item2,index2) in item1.attrsList" :key="index2">{{item2.propertyName}}：{{item2.propertyValue}};</text>
								</view>
								<view class="">
									<view class="priceText">
										<text>{{(item1.price+'').split('.')[0]}}</text>
										<text>.{{(item1.price+'').split('.')[1]}}</text>
									</view>
									<text>商品</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

		</scroll-view>
		<view class="shoppingCartBot">
			<view class="left">
				<view class="" @click="handeAll">
					<image
						:src="data.result.list.map(item=>item.products.filter(item=>item.bool)).map(item=>item.length).reduce((a,b)=>a+b,0) == data.result.list.map(item=>item.products).map(item=>item.length).reduce((a,b)=>a+b,0)?'/static/img/index/49.png':'/static/img/index/52.png'"
						mode="widthFix"></image>
					全选
				</view>
				<view class="">
					<view class="">
						实付合计:
						<text>¥{{data.result.list.map(item=>item.products.filter(item=>item.bool).map(item=>(item.price*1) * (item.num*1))).map(item=>item.reduce((a,b)=>a+b,0)).reduce((a,b)=>a+b,0)}}</text>
					</view>
					已选{{data.result.list.map(item=>item.products.filter(item=>item.bool)).map(item=>item.length).reduce((a,b)=>a+b,0)}}件
				</view>
			</view>
			<view @tap="handeSettlement" class="right">
				去结算({{data.result.list.map(item=>item.products.filter(item=>item.bool)).map(item=>item.length).reduce((a,b)=>a+b,0)}})
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import {
		reactive,
	} from 'vue';
	import { pageShopping,deleteShopping } from '@/utils/api/goods'
	import NavBar from '@/components/navBar.vue'
	const data = reactive<any>({
		result: {},
		obj: { pageNum: 1, pageSize: 10 },
		isAll: false,
		httpUrl: getApp().globalData.baseUrl,
	})
	const navTo = (url : string) => {
		uni.navigateTo({
			url
		})
	}
	const handeAllItem = (item) => {
		const length = item.products.filter(item => !item.bool).length;
		item.products.map((item) => {
			item.bool = length ? true : false
		})
	}
	const handeAll = () => {
		if (data.result.list.map(item => item.products.filter(item => item.bool)).map(item => item.length).reduce((a, b) => a + b, 0) == data.result.list.map(item => item.products).map(item => item.length).reduce((a, b) => a + b, 0)) {
			data.result.list.map((item) => {
				item.products.map((item1) => {
					item1.bool = false;
				})
			})
		} else {
			data.result.list.map((item) => {
				item.products.map((item1) => {
					item1.bool = true;
				})
			})
		}
	}
	const longpress = (item,index,index1)=>{
		uni.showActionSheet({
			itemList: [ '删除'],
			success: (e) => {
				uni.showModal({
					title:'提示',
					content:'是否删除该商品?',
					success:(e)=>{
						if(e.confirm){
							uni.showLoading({
								title:'加载中...',
								mask:true
							})
							deleteShopping({data:{id:item.products[index1].id}}).then((res)=>{
								if(res.code == 200){
									uni.showToast({
										title:'删除成功!',
										icon:'none'
									})
									item.products.splice(index1,1);
									if(!item.products.length){
										data.result.list.splice(index,1)
									}
								}
							})
						}
					}
				})
			}
		})
	}
	const handeSettlement = () => {
		if (!data.result.list.map(item => item.products.filter(item => item.bool)).map(item => item.length).reduce((a, b) => a + b, 0)) {
			uni.showToast({
				title: '请选择商品!',
				icon: 'none'
			})
			return;
		}
		uni.redirectTo({
			url:"/pages/confirmOrder/index"
		})
		setTimeout(() => {
			uni.$emit('confirmOrder', data.result.list.map(item => {
				return {
					name:item.storeName,
					list:item.products.filter(item => item.bool)
				}
				
			}))
		}, 500)
	}
	const requstList = () => {
		if (data.obj.pageNum == 1) {
			data.result.list = [];
		}
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
		pageShopping({ data: data.obj }).then((res) => {
			res.data.list.map((item) => {
				item.products.map((item1) => {
					item1.price  = item1.price + '';
					item1.preferentialPrice = item1.preferentialPrice+''
					const index = item1.price.indexOf('.');
					const index1 = item1.preferentialPrice.indexOf('.');
					console.log(index1,'item1.price')
					if (index1 == -1) {
						item1.preferentialPrice = item1.preferentialPrice + '.00'
					}
					if (index == -1) {
						item1.price = item1.price + '.00'
					}
					if(item1.preferentialPrice * 1){
						item1.price = item1.preferentialPrice
					}
				})
			})
			console.log(res.data.list,'res.data.list')
			res.data.list = [...data.result.list, ...res.data.list];
			data.result = res.data;
			uni.hideLoading()
		})
	}
	requstList()
</script>

<style lang="scss">
	:deep(.navBarTop) {
		background: transparent !important;
	}

	.top_img {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;

		image {
			width: 100%;
		}
	}

	.scrollView {
		>view {
			margin-top: 0 !important;
		}
	}

	.shoppingCartItem {
		padding: 30rpx 24rpx;
		border-radius: 24rpx;
		background: #fff;
		margin: 24rpx 24rpx 0 24rpx;
		margin-bottom: 24rpx;

		>.top {
			display: flex;
			align-items: center;
			font-size: 32rpx;
			margin-bottom: 24rpx;

			image {
				width: 36rpx;
				margin-right: 24rpx;
			}
		}

		>view:nth-of-type(2) {
			margin-top: 0 !important;
		}

		.shoppingCartItem_1 {
			display: flex;
			margin-top: 50rpx;
			align-items: center;

			>image {
				width: 36rpx;
				margin-right: 24rpx;
			}

			>.right {
				flex: 1;
				display: flex;
				align-items: center;

				>.left {
					width: 180rpx;
					margin-right: 24rpx;
					height: 180rpx;
					border-radius: 24rpx;
					background: #ccc;
					position: relative;

					text {
						display: inline-block;
						padding: 5rpx 16rpx;
						background: #CD9A67;
						color: #FFF5E2;
						font-size: 20rpx;
						border-radius: 24rpx 0 24rpx 0;
						position: absolute;
						left: 0;
						top: 0;
						z-index: 9;
					}
					image{
						width: 100%;
						height: 100%;
						border-radius: 24rpx;
					}
				}

				>.right {
					flex: 1;
					width: 0;
					display: flex;
					flex-direction: column;

					>text:nth-of-type(1) {
						line-height: 1.5;
					}

					>text:nth-of-type(2) {
						font-size: 24rpx;
						color: #8C8C8C;
						margin: 8rpx 0;
					}
					.attr {
						
						text{
							font-size: 24rpx;
							color: #8C8C8C;
							display: inline-block;
							margin-right: 10rpx;
						}
					}

					>view:nth-of-type(2) {
						display: flex;
						align-items: center;
						justify-content: space-between;

						>text {
							width: 95rpx;
							line-height: 48rpx;
							text-align: center;
							font-size: 24rpx;
							color: #6B64D3;
							background: rgb(244, 233, 255);
						}
					}
				}
			}
		}
	}

	.shoppingCartBot {
		background: #fff;
		padding: 16rpx 16rpx 50rpx 16rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 24rpx;

		.left {
			display: flex;
			align-items: center;

			>view:nth-of-type(1) {
				display: flex;
				align-items: center;
				font-size: 24rpx;
				margin-right: 16rpx;

				image {
					width: 36rpx;
					margin-right: 15rpx;
				}
			}

			>view:nth-of-type(2) {
				color: #8C8C8C;
				font-size: 24rpx;

				view {
					margin-bottom: 8rpx;
				}

				text {
					font-weight: bold;
				}
			}
		}

		.right {
			width: 240rpx;
			line-height: 80rpx;
			border-radius: 40rpx;
			text-align: center;
			font-size: 32rpx;
			color: #fff;
			background: #6B64D3;
		}
	}

	.shoppingCart {
		height: 100vh;
		background: #F6F7F9;
		display: flex;
		flex-direction: column;
	}

	.topRight {
		display: flex;
		align-items: center;

		image {
			width: 36rpx;
			margin-right: 10rpx;
		}
	}

	scroll-view {
		padding-top: 41rpx;
		flex: 1;
		overflow: auto;

	}
</style>